package com.ipan.builder.freemarker;

import java.util.Locale;

import com.ipan.builder.BuilderCfg;
import com.ipan.builder.ognl.ValueStack;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;

/**
 * Freemarker管理器
 * 
 * @author iPan
 * @version 2013-12-1
 */
public class FreemarkerManager {
	public static final String DEFAULT_ENCODING = "utf-8";
	private static Configuration DEFAULT_CONFIG = null;
	private static ObjectWrapper DEFAULT_WRAPPER = ObjectWrapper.DEFAULT_WRAPPER;

	public static synchronized Configuration getDefaultConfiguration() {
		if (DEFAULT_CONFIG == null) {
			DEFAULT_CONFIG = new Configuration();
			DEFAULT_CONFIG.setDefaultEncoding(DEFAULT_ENCODING);
			DEFAULT_CONFIG.setEncoding(Locale.getDefault(), DEFAULT_ENCODING);
			DEFAULT_CONFIG.setClassForTemplateLoading(FreemarkerManager.class, BuilderCfg.getInstance().getTemplateClassPath());
		}
		return DEFAULT_CONFIG;
	}
	
	public static Configuration createConfiguration() {
		return new Configuration();
	}
	
	public static OgnlHashModel createOgnlHashModel(ObjectWrapper objectWrapper, ValueStack stack) {
		return new OgnlHashModel(objectWrapper, stack);
	}
	
	public static OgnlHashModel createOgnlHashModel(ObjectWrapper objectWrapper) {
		return new OgnlHashModel(objectWrapper);
	}
	
	public static OgnlHashModel createOgnlHashModel(ValueStack stack) {
		return new OgnlHashModel(DEFAULT_WRAPPER, stack);
	}
	
}
